home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / lib / vector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-27  |  4.5 KB  |  273 lines

  1. /* vector.c: vector/point operations.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "vector.h"
  22.  
  23.  
  24. /* Given the point COORD, return the corresponding vector.  */
  25.  
  26. const vector_type
  27. make_vector (const real_coordinate_type c)
  28. {
  29.   vector_type v;
  30.  
  31.   v.dx = c.x;
  32.   v.dy = c.y;
  33.  
  34.   return v;
  35. }
  36.  
  37.  
  38. /* And the converse: given a vector, return the corresponding point.  */
  39.  
  40. const real_coordinate_type
  41. vector_to_point (const vector_type v)
  42. {
  43.   real_coordinate_type coord;
  44.  
  45.   coord.x = v.dx;
  46.   coord.y = v.dy;
  47.  
  48.   return coord;
  49. }
  50.  
  51.  
  52.  
  53. const real
  54. magnitude (const vector_type v)
  55. {
  56.   return hypot (v.dx, v.dy);
  57. }
  58.  
  59.  
  60. const vector_type
  61. normalize (const vector_type v)
  62. {
  63.   vector_type new_v;
  64.   real m = magnitude (v);
  65.  
  66.   assert (m > 0.0);
  67.  
  68.   new_v.dx = v.dx / m;
  69.   new_v.dy = v.dy / m;
  70.  
  71.   return new_v;
  72. }
  73.  
  74.  
  75. const vector_type
  76. Vadd (const vector_type v1, const vector_type v2)
  77. {
  78.   vector_type new_v;
  79.  
  80.   new_v.dx = v1.dx + v2.dx;
  81.   new_v.dy = v1.dy + v2.dy;
  82.  
  83.   return new_v;
  84. }
  85.  
  86.  
  87. const real
  88. Vdot (const vector_type v1, const vector_type v2)
  89. {
  90.   return v1.dx * v2.dx + v1.dy * v2.dy;
  91. }
  92.  
  93.  
  94. const vector_type
  95. Vmult_scalar (const vector_type v, const real r)
  96. {
  97.   vector_type new_v;
  98.  
  99.   new_v.dx = v.dx * r;
  100.   new_v.dy = v.dy * r;
  101.  
  102.   return new_v;
  103. }
  104.  
  105.  
  106. /* Given the IN_VECTOR and OUT_VECTOR, return the angle between them in
  107.    degrees, in the range zero to 180.  */
  108.    
  109. real
  110. Vangle (const vector_type in_vector, const vector_type out_vector)
  111. {
  112.   vector_type v1 = normalize (in_vector);
  113.   vector_type v2 = normalize (out_vector);
  114.  
  115.   return acosd (Vdot (v2, v1));
  116. }
  117.  
  118.  
  119. const real_coordinate_type
  120. Vadd_point (const real_coordinate_type c, const vector_type v)
  121. {
  122.   real_coordinate_type new_c;
  123.  
  124.   new_c.x = c.x + v.dx;
  125.   new_c.y = c.y + v.dy;
  126.   return new_c;
  127. }
  128.  
  129.  
  130. const real_coordinate_type
  131. Vsubtract_point (const real_coordinate_type c, const vector_type v)
  132. {
  133.   real_coordinate_type new_c;
  134.  
  135.   new_c.x = c.x - v.dx;
  136.   new_c.y = c.y - v.dy;
  137.   return new_c;
  138. }
  139.  
  140.  
  141. const coordinate_type
  142. Vadd_int_point (const coordinate_type c, const vector_type v)
  143. {
  144.   coordinate_type a;
  145.   
  146.   a.x = ROUND ((real) c.x + v.dx);
  147.   a.y = ROUND ((real) c.y + v.dy);
  148.   return a;
  149. }
  150.  
  151.  
  152. const vector_type
  153. Vabs (const vector_type v)
  154. {
  155.   vector_type new_v;
  156.   
  157.   new_v.dx = fabs (v.dx);
  158.   new_v.dy = fabs (v.dy);
  159.   return new_v;
  160. }
  161.  
  162.  
  163.  
  164. /* Operations on points.  */
  165.  
  166. /* This is a macro now.  */
  167. #if 0
  168. const real_coordinate_type
  169. Padd (real_coordinate_type coord1, real_coordinate_type coord2)
  170. {
  171.   real_coordinate_type sum;
  172.  
  173.   sum.x = coord1.x + coord2.x;
  174.   sum.y = coord1.y + coord2.y;
  175.  
  176.   return sum;
  177. }
  178.  
  179.  
  180. const real_coordinate_type
  181. Pmult_scalar (const real_coordinate_type coord, const real r)
  182. {
  183.   real_coordinate_type answer;
  184.  
  185.   answer.x = coord.x * r;
  186.   answer.y = coord.y * r;
  187.  
  188.   return answer;
  189. }
  190. #endif /* 0 */
  191.  
  192.  
  193. const vector_type
  194. Psubtract (const real_coordinate_type c1, const real_coordinate_type c2)
  195. {
  196.   vector_type v;
  197.  
  198.   v.dx = c1.x - c2.x;
  199.   v.dy = c1.y - c2.y;
  200.  
  201.   return v;
  202. }
  203.  
  204.  
  205.  
  206. /* Operations on integer points.  */
  207.  
  208. const vector_type
  209. IPsubtract (const coordinate_type coord1, const coordinate_type coord2)
  210. {
  211.   vector_type v;
  212.  
  213.   v.dx = coord1.x - coord2.x;
  214.   v.dy = coord1.y - coord2.y;
  215.  
  216.   return v;
  217. }
  218.  
  219.  
  220. const coordinate_type
  221. IPsubtractP (const coordinate_type c1, const coordinate_type c2)
  222. {
  223.   coordinate_type c;
  224.   
  225.   c.x = c1.x - c2.x;
  226.   c.y = c1.y - c2.y;
  227.   
  228.   return c;
  229. }
  230.  
  231.  
  232. const coordinate_type
  233. IPadd (const coordinate_type c1, const coordinate_type c2)
  234. {
  235.   coordinate_type c;
  236.   
  237.   c.x = c1.x + c2.x;
  238.   c.y = c1.y + c2.y;
  239.   
  240.   return c;
  241. }
  242.  
  243.  
  244. const coordinate_type
  245. IPmult_scalar (const coordinate_type c, const int i)
  246. {
  247.   coordinate_type a;
  248.   
  249.   a.x = c.x * i;
  250.   a.y = c.y * i;
  251.   
  252.   return a;
  253. }
  254.  
  255.  
  256. const real_coordinate_type
  257. IPmult_real (const coordinate_type c, const real r)
  258. {
  259.   real_coordinate_type a;
  260.  
  261.   a.x = c.x * r;
  262.   a.y = c.y * r;
  263.  
  264.   return a;
  265. }
  266.  
  267.  
  268. const boolean
  269. IPequal (const coordinate_type c1, const coordinate_type c2)
  270. {
  271.   return c1.x == c2.x && c1.y == c2.y;
  272. }
  273.